package com.waypolice.manager.service.impl;

import com.waypolice.manager.service.ActivitiService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Model;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author zyp
 * @description activiti业务层实现类
 * @create 2019-02-25 16:32
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ActivitiServiceImpl implements ActivitiService {

    /**
     * @author:zyp
     * @description 实现工作流模型分页
     * @date: 2019/2/26 9:30
     * @return 
     */
    @Override
    public List<Model> pagingActModel(List<Model> models, int offset, int limit, String search) {

        //临时的list集合
        List<Model>  provisionalList =  new ArrayList<Model>();
        //最终的list集合
        List<Model> finalList = new ArrayList<Model>();
        if(StringUtils.isNotBlank(search)){
            for (Model model : models){
                if (model.getName()!=null&&model.getName().contains(search)){
                    provisionalList.add(model);
                }
            }
        }else{
            provisionalList = models;
        }
        if((limit+offset)<=provisionalList.size()){
            for (int i=offset;i<limit+offset;i++){
                finalList.add(provisionalList.get(i));
            }
        }else{
            for (int i=offset;i<provisionalList.size();i++){
                finalList.add(provisionalList.get(i));
            }
        }
        return finalList;
    }



    /**
     * @author:zyp
     * @description 实现工作流部署分页
     * @date: 2019/2/26 9:30
     * @return 
     */
    @Override
    public List<Map<String, Object>> pagingActDeploy(List<Map<String, Object>> deploys, int offset, int limit, String search) {
        //临时的list集合
        List<Map<String, Object>>  middleList =  new ArrayList<Map<String, Object>>();
        //最终的list集合
        List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
        if(StringUtils.isNotBlank(search)){
            for (Map<String, Object> deploy : deploys){
                if (deploy.get("name")!=null&&String.valueOf(deploy.get("name")).contains(search)){
                    middleList.add(deploy);
                }
            }
        }else{
            middleList = deploys;
        }
        int num = limit+offset;
        if(num<=middleList.size()){
            for (int i=offset;i<num;i++){
                finalList.add(middleList.get(i));
            }
        }else{
            for (int i=offset;i<middleList.size();i++){
                finalList.add(middleList.get(i));
            }
        }
        return finalList;
    }

    /**
     * @author:zyp
     * @description 针对用户与组中的用户进行分页与排序
     * @date: 2019/2/26 9:30
     * @return 
     */
    @Override
    public List<User> pagingActUser(List<User> users, int offset, int limit, String search) {
        //临时的list集合
        List<User>  userList =  new ArrayList<User>();
        //最终的list集合
        List<User> finalList = new ArrayList<User>();
        if(StringUtils.isNotBlank(search)){
            for (User user : users){
                if (user.getFirstName()!=null&&user.getFirstName().contains(search)){
                    userList.add(user);
                }
            }
        }else{
            userList = users;
        }
        int num =limit+offset;
        if(num<=userList.size()){
            for (int i=offset;i<limit+offset;i++){
                finalList.add(userList.get(i));
            }
        }else{
            for (int i=offset;i<userList.size();i++){
                finalList.add(userList.get(i));
            }
        }

        return finalList;
    }

    /**
     * @author:zyp
     * @description 针对用户与组中的组进行分页与排序
     * @date: 2019/2/26 10:12
     * @return 
     */
    @Override
    public List<Group> pagingActGroup(List<Group> groups, int offset, int limit, String search) {
        //临时的list集合
        List<Group>  groupList =  new ArrayList<Group>();
        //最终的list集合
        List<Group> finalList = new ArrayList<Group>();
        if(StringUtils.isNotBlank(search)){
            for (Group group : groups){
                if (group.getName()!=null&&group.getName().contains(search)){
                    groupList.add(group);
                }
            }
        }else{
            groupList = groups;
        }
        int groupNum =limit+offset;
        if(groupNum<=groupList.size()){
            int i=offset;
            for (;i<limit+offset;){
                finalList.add(groupList.get(i));
                i++;
            }
        }else{
            for (int i=offset;i<groupList.size();i++){
                finalList.add(groupList.get(i));
            }
        }

        return finalList;
    }
}